package com.kgc.fk.Controller;


import com.kgc.basic.util.ResponseMsg;
import com.kgc.fk.Feign.CwCGDDConsumerFeign;
import com.kgc.fk.Service.PrePayServ;
import com.kgc.fk.Service.PublicConServ;
import com.kgc.skconsumer.feign.CwWorkFlowFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("YFK")
public class PrePayController {
    private  Logger logger  = LoggerFactory.getLogger(PrePayController.class);

    @Resource
    private PrePayServ prePayServ;

    @Resource
    private PublicConServ publicConServ;

    @Resource
    private CwWorkFlowFeign cwWorkFlowFeign;

    @Resource
    private CwCGDDConsumerFeign cwCGDDFeign;

    @RequestMapping("getPage")
    public ResponseMsg getPage(@RequestBody Map<String,Object> map){
        ResponseMsg responseMsg = prePayServ.getPrePayPage(map);
        return responseMsg;
    }

    @RequestMapping("addPrePay")
    public ResponseMsg addPrePay(@RequestBody Map<String,Object> map){
        logger.info("addPrePay map{}",map);
        ResponseMsg responseMsg = prePayServ.addPrePay(map);
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",responseMsg.getData());//单据编号
        variables.put("type",map.get("type"));//流程类型
        //创建一个流程实例
        com.kgc.activiti.util.ResponseMsg res = cwWorkFlowFeign.startProcess(variables);
        if (!"200".equals(res.getCode())){
            responseMsg.setCode(res.getCode());
        }
        return responseMsg;
    }

    @RequestMapping("delPrePay")
    public ResponseMsg delPrepay(String code){
        logger.info("delPrePay code{}",code);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",code);
        map.put("del",1);
        ResponseMsg responseMsg = prePayServ.updatePrePay(map);
        return responseMsg;
    }
    @RequestMapping("getPrePay")
    public ResponseMsg getPrePay(@RequestBody Map<String,Object> map){
        logger.info("getPrePay map{}",map);
        ResponseMsg responseMsg = prePayServ.getPrePay(map);
        return responseMsg;
    }

    @RequestMapping("modiPrePay")
    public ResponseMsg modiPrePay(@RequestBody Map<String,Object> map){
        logger.info("modiPrePay map{}",map);
        ResponseMsg responseMsg = prePayServ.modiPrePay(map);
        return responseMsg;
    }

    /**
     *  pro_id
     * @param
     * @return
     */
    @RequestMapping("getPrePayList")
    public ResponseMsg getPrePayList(@RequestBody Map<String,Object> map){
        logger.info("getPrePayList map{}",map);
        ResponseMsg responseMsg = prePayServ.getPrePayList(map);
        return responseMsg;
    }

    @RequestMapping("/check")
    public  ResponseMsg check(@RequestBody Map<String,Object> map){
        ResponseMsg responseMsg = new ResponseMsg();
        logger.info("PrepayController check params,{}",map.toString());

        Map<String , Object> prePay = (Map<String, Object>) prePayServ.getPrePay(map).getData();

        Map<String , Object> cgdd = (Map<String, Object>) cwCGDDFeign.getCGDD((String)prePay.get("sourceSheet")).getData();
        if ("11".equals(cgdd.get("status").toString())){
            responseMsg.setCode("500");
            responseMsg.setMsg("源单已经关闭");
            return responseMsg;
        }


        Map<String,Object> variables = new HashMap<>();
        variables.put("code",map.get("code"));//单据编号
        variables.put("checkManId",Integer.valueOf((String) map.get("checkManId")));//审批人
        variables.put("checkManName",(String) map.get("checkManName"));//审批人
        variables.put("checkComment",(String) map.get("checkComment"));//审批意见
        variables.put("checkDate",new Date());//审批时间
        variables.put("checkRes",(boolean)map.get("checkRes"));//审批结果
        com.kgc.activiti.util.ResponseMsg msg=  cwWorkFlowFeign.competeTask(variables);
        responseMsg.setCode(msg.getCode());
        responseMsg.setMsg(msg.getMsg());
        return responseMsg;
    }
}
